package grape.mybatisplus.modules;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import grape.mybatisplus.generator.CustomerDataSourceConfig;
import grape.mybatisplus.generator.SuperGenerator;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static grape.mybatisplus.generator.SelfFileOutConfig.dot_mvc;

/**
 * crm 模块生成
 * Created by yangwei
 * Created at 2019/7/22 18:40
 */
@Data
@EqualsAndHashCode(callSuper=false)
public class CrmGenerator extends SuperGenerator {

    //静态变量，作为key使用，不宜修改
    private static String author="author";
    private static String moduleName="moduleName";
    private static String tableName="tableName";
    private static String tableType="tableType";
    private static String tablePrefix="tablePrefix";
    private static String vueProjectPathKey="vueProjectPathKey";
    private static String requestPrefix="requestPrefix";

    public static void main(String[] args) {


        String parentPakage="grape";
        String parentModule = "/modules/crm";
        String restModeule = "/crm-rest";
        String serviceModeule = "/crm-service";
        String serviceApiModule = "/crm-service-api";

        for (Map<String, String> stringStringMap : tableConfig()) {
            CrmGenerator generator = new CrmGenerator();

            generator.setAuthor(stringStringMap.get(author));

            generator.setTableName(stringStringMap.get(tableName));
            generator.setModuleName(stringStringMap.get(moduleName));
            generator.setTableType(TableType.valueOf(stringStringMap.get(tableType)));
            //取表名第一组作为前缀
            generator.setTablePrefix(stringStringMap.get(tablePrefix));
            generator.setRequestPrefix(stringStringMap.get(requestPrefix));

            String serviceImplModule = serviceApiModule.replace("-api","-impl");

            generator.setVueProjectPath(stringStringMap.get(vueProjectPathKey));
            generator.setControllerModulePath(parentModule + restModeule);
            generator.setServiceApiModulePath(parentModule + serviceModeule + serviceApiModule);
            generator.setServiceImplModulePath(parentModule + serviceModeule + serviceImplModule);

            String serviceApiParentPakage = parentPakage + replace(serviceModeule);
            String serviceImplParentPakage = serviceApiParentPakage;


            generator.setControllerPakage(parentPakage + replace(restModeule) + "." + generator.getModuleName() + dot_mvc);
            generator.setServicePakage(serviceApiParentPakage + "." + generator.getModuleName() + ".api");
            generator.setServiceImplPakage(serviceImplParentPakage + "." + generator.getModuleName() + ".impl");
            generator.setPoPakage(serviceApiParentPakage + "." + generator.getModuleName() + ".po");
            generator.setMapperPakage(serviceImplParentPakage + "." + generator.getModuleName() + ".mapper");

            generator.doGenerate();
        }


    }
    static List<Map<String,String>> tableConfig = new ArrayList<>();
    public static List<Map<String,String>> tableConfig(){
        String author = "yangwei";
        String tablePrefix = "crm";
        String requestPrefix = tablePrefix;

        // 以下注释了的说明生成的个别文件被移动或删除了，再次生成会重新生成，所以注释了
/*
        commonSet("crm_company","company","/web/crm",requestPrefix,TableType.tree,tablePrefix,author);
        commonSet("crm_customer","customer","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_customer_belong","customerbelong","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_customer_contact","customercontact","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_customer_credential","customercredential","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_customer_relation","customerrelation","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_department","department","/web/crm",requestPrefix,TableType.tree,tablePrefix,author);
        commonSet("crm_relation","relation","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_relation_detail","relationdetail","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_tag","tag","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);
        commonSet("crm_customer_tag","customertag","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);*/
        commonSet("crm_customer_associated_contact_person","customerassociatedcontactperson","/web/crm",requestPrefix,TableType.normal,tablePrefix,author);

        return  tableConfig;
    }
    // vue路径添加
    private static void commonSet(String tableName,String moduleName,String vueProjectPath,String requestPrefix,TableType tableType,String tablePrefix,String author){
        Map<String, String> tableConfigItem;
        tableConfigItem = new HashMap<>();
        tableConfigItem.put(CrmGenerator.tableName,tableName);
        tableConfigItem.put(CrmGenerator.moduleName,moduleName);
        tableConfigItem.put(CrmGenerator.tableType,tableType.name());
        tableConfigItem.put(CrmGenerator.tablePrefix,tablePrefix);
        tableConfigItem.put(CrmGenerator.author,author);
        tableConfigItem.put(CrmGenerator.vueProjectPathKey,vueProjectPath);
        tableConfigItem.put(CrmGenerator.requestPrefix,requestPrefix);
        tableConfig.add(tableConfigItem);
    }

    /**
     * 数据源配置
     * @return
     */
    public DataSourceConfig dataSourceConfig(){


        CustomerDataSourceConfig dsc = new CustomerDataSourceConfig();
        dsc.setUrl("jdbc:mysql://db.grape.com/grape_crm?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("grape");
        dsc.setPassword("KT47fndyUcDZ+lbepS1akd1iC90jA5nRvN7CXrek0DLPz7dtFzCGydnNwKNsl86P3pPT9DS/I0KV3u2p13CbXg==");
        dsc.setPublickKey("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJFe6ifhmL6pTd4wSfcwL5hxprIQEp4JpnWce4ss+kKjxHS0Pw+u61L/BFy8lZgtSa4A3DmSsqg/DJdmPKz8H9UCAwEAAQ==");
        return dsc;
    }
    private static String replace(String str){
        return str.replace("/",".").replace("-",".");
    }

}
